home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Unix / rpc⁄xdr.h < prev    next >
Encoding:
Text File  |  1990-04-09  |  8.1 KB  |  249 lines  |  [TEXT/????]

  1. /*      @(#)xdr.h 1.1 86/09/24 SMI      */
  2.  
  3. /*
  4.  * xdr.h, External Data Representation Serialization Routines.
  5.  *
  6.  * Copyright (C) 1984, Sun Microsystems, Inc.
  7.  */
  8.  
  9. #ifndef __XDR_HEADER__
  10. #define __XDR_HEADER__
  11.  
  12. /*
  13.  * XDR provides a conventional way for converting between C data
  14.  * types and an external bit-string representation.  Library supplied
  15.  * routines provide for the conversion on built-in C data types.  These
  16.  * routines and utility routines defined here are used to help implement
  17.  * a type encode/decode routine for each user-defined type.
  18.  *
  19.  * Each data type provides a single procedure which takes two arguments:
  20.  *
  21.  *    bool_t
  22.  *    xdrproc(xdrs, argresp)
  23.  *        XDR *xdrs;
  24.  *        <type> *argresp;
  25.  *
  26.  * xdrs is an instance of a XDR handle, to which or from which the data
  27.  * type is to be converted.  argresp is a pointer to the structure to be
  28.  * converted.  The XDR handle contains an operation field which indicates
  29.  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
  30.  *
  31.  * XDR_DECODE may allocate space if the pointer argresp is null.  This
  32.  * data can be freed with the XDR_FREE operation.
  33.  *
  34.  * We write only one procedure per data type to make it easy
  35.  * to keep the encode and decode procedures for a data type consistent.
  36.  * In many cases the same code performs all operations on a user defined type,
  37.  * because all the hard work is done in the component type routines.
  38.  * decode as a series of calls on the nested data types.
  39.  */
  40.  
  41. /*
  42.  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
  43.  * stream.  XDR_DECODE causes the type to be extracted from the stream.
  44.  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
  45.  * request.
  46.  */
  47. enum xdr_op {
  48.     XDR_ENCODE=0,
  49.     XDR_DECODE=1,
  50.     XDR_FREE=2,
  51.     XDR_4BYTES=0x7fffffff
  52. };
  53.  
  54. /*
  55.  * This is the number of bytes per unit of external data.
  56.  */
  57. #define BYTES_PER_XDR_UNIT    (4)
  58. #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
  59.             * BYTES_PER_XDR_UNIT)
  60.  
  61. /*
  62.  * A xdrproc_t exists for each data type which is to be encoded or decoded.
  63.  *
  64.  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
  65.  * The opaque pointer generally points to a structure of the data type
  66.  * to be decoded.  If this pointer is 0, then the type routines should
  67.  * allocate dynamic storage of the appropriate size and return it.
  68.  * bool_t    (*xdrproc_t)(XDR *, caddr_t *);
  69.  */
  70. typedef    bool_t (*xdrproc_t)();
  71.  
  72. /*
  73.  * The XDR handle.
  74.  * Contains operation which is being applied to the stream,
  75.  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
  76.  * and two private fields for the use of the particular impelementation.
  77.  */
  78. typedef struct {
  79.     enum xdr_op    x_op;        /* operation; fast additional param */
  80.     struct xdr_ops {
  81.         bool_t    (*x_getlong)();    /* get a long from underlying stream */
  82.         bool_t    (*x_putlong)();    /* put a long to " */
  83.         bool_t    (*x_getbytes)();/* get some bytes from " */
  84.         bool_t    (*x_putbytes)();/* put some bytes to " */
  85.         u_int    (*x_getpostn)();/* returns bytes off from beginning */
  86.         bool_t  (*x_setpostn)();/* lets you reposition the stream */
  87.         long *    (*x_inline)();    /* buf quick ptr to buffered data */
  88.         void    (*x_destroy)();    /* free privates of this xdr_stream */
  89.     } *x_ops;
  90.     caddr_t     x_public;    /* users' data */
  91.     caddr_t        x_private;    /* pointer to private data */
  92.     caddr_t     x_base;        /* private used for position info */
  93.     int        x_handy;    /* extra private word */
  94. } XDR;
  95.  
  96. /*
  97.  * Operations defined on a XDR handle
  98.  *
  99.  * XDR        *xdrs;
  100.  * long        *longp;
  101.  * caddr_t     addr;
  102.  * u_int     len;
  103.  * u_int     pos;
  104.  */
  105. #define XDR_GETLONG(xdrs, longp)            \
  106.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  107. #define xdr_getlong(xdrs, longp)            \
  108.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  109.  
  110. #define XDR_PUTLONG(xdrs, longp)            \
  111.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  112. #define xdr_putlong(xdrs, longp)            \
  113.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  114.  
  115. #define XDR_GETBYTES(xdrs, addr, len)            \
  116.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  117. #define xdr_getbytes(xdrs, addr, len)            \
  118.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  119.  
  120. #define XDR_PUTBYTES(xdrs, addr, len)            \
  121.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  122. #define xdr_putbytes(xdrs, addr, len)            \
  123.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  124.  
  125. #define XDR_GETPOS(xdrs)                \
  126.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  127. #define xdr_getpos(xdrs)                \
  128.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  129.  
  130. #define XDR_SETPOS(xdrs, pos)                \
  131.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  132. #define xdr_setpos(xdrs, pos)                \
  133.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  134.  
  135. #define    XDR_INLINE(xdrs, len)                \
  136.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  137. #define    xdr_inline(xdrs, len)                \
  138.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  139.  
  140. #define    XDR_DESTROY(xdrs)                \
  141.     if ((xdrs)->x_ops->x_destroy)             \
  142.         (*(xdrs)->x_ops->x_destroy)(xdrs)
  143. #define    xdr_destroy(xdrs)                \
  144.     if ((xdrs)->x_ops->x_destroy)             \
  145.         (*(xdrs)->x_ops->x_destroy)(xdrs)
  146.  
  147. /*
  148.  * Support struct for discriminated unions.
  149.  * You create an array of xdrdiscrim structures, terminated with
  150.  * a entry with a null procedure pointer.  The xdr_union routine gets
  151.  * the discriminant value and then searches the array of structures
  152.  * for a matching value.  If a match is found the associated xdr routine
  153.  * is called to handle that part of the union.  If there is
  154.  * no match, then a default routine may be called.
  155.  * If there is no match and no default routine it is an error.
  156.  */
  157. #define NULL_xdrproc_t ((xdrproc_t)0)
  158. struct xdr_discrim {
  159.     int    value;
  160.     xdrproc_t proc;
  161. };
  162.  
  163. /*
  164.  * In-line routines for fast encode/decode of primitve data types.
  165.  * Caveat emptor: these use single memory cycles to get the
  166.  * data from the underlying buffer, and will fail to operate
  167.  * properly if the data is not aligned.  The standard way to use these
  168.  * is to say:
  169.  *    if ((buf = XDR_INLINE(xdrs, count)) == NULL)
  170.  *        return (FALSE);
  171.  *    <<< macro calls >>>
  172.  * where ``count'' is the number of bytes of data occupied
  173.  * by the primitive data types.
  174.  *
  175.  * N.B. and frozen for all time: each data type here uses 4 bytes
  176.  * of external representation.
  177.  */
  178. #define IXDR_GET_LONG(buf)        ntohl(*buf++)
  179. #define IXDR_PUT_LONG(buf, v)        (*buf++ = htonl(v))
  180.  
  181. #define IXDR_GET_BOOL(buf)        ((bool_t)IXDR_GET_LONG(buf))
  182. #define IXDR_GET_ENUM(buf, t)        ((t)IXDR_GET_LONG(buf))
  183. #define IXDR_GET_U_LONG(buf)        ((u_long)IXDR_GET_LONG(buf))
  184. #define IXDR_GET_SHORT(buf)        ((short)IXDR_GET_LONG(buf))
  185. #define IXDR_GET_U_SHORT(buf)        ((u_short)IXDR_GET_LONG(buf))
  186.  
  187. #define IXDR_PUT_BOOL(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  188. #define IXDR_PUT_ENUM(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  189. #define IXDR_PUT_U_LONG(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  190. #define IXDR_PUT_SHORT(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  191. #define IXDR_PUT_U_SHORT(buf, v)    IXDR_PUT_LONG((buf), ((long)(v)))
  192.  
  193. /*
  194.  * These are the "generic" xdr routines.
  195.  */
  196. extern bool_t    xdr_void();
  197. extern bool_t    xdr_int();
  198. extern bool_t    xdr_u_int();
  199. extern bool_t    xdr_long();
  200. extern bool_t    xdr_u_long();
  201. extern bool_t    xdr_short();
  202. extern bool_t    xdr_u_short();
  203. extern bool_t    xdr_bool();
  204. extern bool_t    xdr_enum();
  205. extern bool_t    xdr_array();
  206. extern bool_t    xdr_bytes();
  207. extern bool_t    xdr_opaque();
  208. extern bool_t    xdr_string();
  209. extern bool_t    xdr_union();
  210. #ifndef KERNEL
  211. extern bool_t    xdr_char();
  212. extern bool_t    xdr_u_char();
  213. extern bool_t    xdr_vector();
  214. extern bool_t    xdr_float();
  215. extern bool_t    xdr_double();
  216. extern bool_t    xdr_reference();
  217. extern bool_t    xdr_pointer();
  218. extern bool_t    xdr_wrapstring();
  219. #endif !KERNEL
  220.  
  221. /*
  222.  * Common opaque bytes objects used by many rpc protocols;
  223.  * declared here due to commonality.
  224.  */
  225. #define MAX_NETOBJ_SZ 1024 
  226. struct netobj {
  227.     u_int    n_len;
  228.     char    *n_bytes;
  229. };
  230. typedef struct netobj netobj;
  231. extern bool_t   xdr_netobj();
  232.  
  233. /*
  234.  * These are the public routines for the various implementations of
  235.  * xdr streams.
  236.  */
  237. extern void   xdrmem_create();        /* XDR using memory buffers */
  238. #ifndef KERNEL
  239. extern void   xdrstdio_create();    /* XDR using stdio library */
  240. extern void   xdrrec_create();        /* XDR pseudo records for tcp */
  241. extern bool_t xdrrec_endofrecord();    /* make end of xdr record */
  242. extern bool_t xdrrec_skiprecord();    /* move to beginning of next record */
  243. extern bool_t xdrrec_eof();        /* true if no more input */
  244. #else
  245. extern void xdrmbuf_init();        /* XDR using kernel mbufs */
  246. #endif !KERNEL
  247.  
  248. #endif !__XDR_HEADER__
  249.